<template>
  <div class="comment con">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span
        class="comment"
        @click="$router.push({ path: '/Comment/' + post.id })"
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ post.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        @click="collect"
        :class="{ red: post.has_star }"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5" v-model="text"></textarea>
      <div>
        <span @click="handlerclick">发 送</span>
        <span @click="handlercancel">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
//收藏文章
import { collect, replyComment } from "@/apis/post";
// 文本域聚焦（自定义指令）
// import { inputfocus } from "@/utils/mydirecties";
export default {
  props: {
    post: {
      type: Object,
      required: true,
    },
    item: {
      default: null,
    },
  },
  //监听父组件传过来的值是否变化
  watch: {
    item() {
      if (this.item) {
        // 弹出文本域
        this.isFocus = true;
        // 让文本域聚焦
        setTimeout(() => {
          this.$refs.commtext.focus();
        }, 10);
      }
    },
  },
  // // 自定义指令
  // directives: {
  //   inputfocus,
  // },
  data() {
    return {
      isFocus: false,
      // 文本域的值
      text: "",
    };
  },
  methods: {
    //   获取焦点时触发
    // handlerFocus() {
    //   this.isFocus = !this.isFocus;
    //   this.$refs.commtext.focus();
    // },
    // 点击收藏
    async collect(e) {
      // 请求
      console.log(this.post);
      let res = await collect(this.post.id);
      console.log(res);
      // 改变页面样式
      this.post.has_star = !this.post.has_star;
      // 提示用户
      this.$toast.success(res.data.message);
    },
    send() {
      console.log(this.$refs.commtext.value);
      // console.log(1);
    },
    //   获取焦点时触发
    handlerFocus() {
      this.isFocus = !this.isFocus;
      // 让文本域聚焦
      setTimeout(() => {
        this.$refs.commtext.focus();
      }, 10);
      // console.log(1);
    },
    // 点击发表评论
    async handlerclick(e) {
      // 去除两边空格
      if (this.text.trim().length == 0) {
        this.$toast("请输入评论");
        return;
      }
      console.log(this.post);
      let data = {
        content: this.text,
      };
      // 如果是单击回复，就会有item对象，说明有上一级
      if (this.item) {
        data.parent_id = this.item.id;
      }
      let res = await replyComment(this.post.id, data);
      // console.log(res);

      if (res.data.message == "评论发布成功") {
        // 刷新页面
        // 给父组件发出事件
        this.$emit("refresh", e);
        // 提示
        this.$toast.success(res.data.message);
        //  弹框收缩
        this.isFocus = !this.isFocus;
        // 清空值
        this.text = "";
      }
    },
    // 取消评论
    handlercancel() {
      // 隐藏文本域
      this.isFocus = false;
      // 清空文本域的值
      this.text = "";
      // 不能在子组件中直接修改props成员的值，这个值只能由父组件进行修改
      this.$emit("reset");
    },
  },
};
</script>

<style lang='less' scoped>
.red {
  color: #f00;
}
.con {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: -1px;
  .addcomment {
    background-color: #fff;
  }
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>
